package com.exp.practice.base.user.controller;

import com.exp.practice.base.exception.MyException;
import com.exp.practice.base.user.domain.User;
import com.exp.practice.base.user.service.UserService;
import com.exp.practice.base.vo.ResultVo;
import com.exp.practice.workbench.domain.Student;
import com.exp.practice.workbench.domain.Teacher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

   @Resource
    private UserService userService;

   @RequestMapping("/login.do")
   @ResponseBody
   public ResultVo login(User user, HttpSession session){
       ResultVo resultVo=new ResultVo();
       try {
           String identity=user.getIdentity();
           if("0".equals(identity)){//登录者为学生
               Student student=userService.stuLogin(user);
               session.setAttribute("user",student);
               resultVo.setSuccess(true);
               resultVo.setMsg("登录成功");
           }else if("1".equals(identity)) {//登录者为教师
               Teacher teacher=userService.teaLogin(user);
               session.setAttribute("user",teacher);
               resultVo.setSuccess(true);
               resultVo.setMsg("登录成功");
           }

       } catch (Exception e) {
          resultVo.setMsg(e.getMessage());
           resultVo.setSuccess(false);
       }
       return resultVo;
   }


}
